package com.example.demo.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.example.demo.constant.SystemConstant;
import com.example.demo.constant.TaskConstant;
import com.example.demo.controller.bean.Result;
import com.example.demo.dto.*;
import com.example.demo.mapper.entity.*;
import com.example.demo.service.RegionService;
import com.example.demo.service.StudentOrderService;
import com.example.demo.service.StudentPointsService;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Lucy
 * @create 2024-02-20 13:40
 */

@RequestMapping("myOrder")
@Api(value = "我的订单", tags = {"我的订单接口"})
@RestController
@Slf4j
public class MyOrderController {

    @Autowired
    private StudentOrderService orderService;

    @Autowired
    private StudentPointsService studentPointsService;

    @Autowired
    private RegionService regionService;

    @ApiOperation("获取待处理订单列表")
    @RequestMapping(value = "getPendingOrderList", method = {RequestMethod.POST})
    public Result<PageVo<OrderListDTO>> getPendingOrderList(@RequestBody PageBo pageBo){
        int studentId = StpUtil.getLoginIdAsInt();
        PageVo<OrderListDTO> pendingOrderList =  orderService.getStatusOrderList(pageBo, studentId, TaskConstant.ORDER_STATUS_WAIT);
        return Result.ok(pendingOrderList);
    }

    @ApiOperation("获取待收货订单列表")
    @RequestMapping(value = "getToReceiveOrderList", method = {RequestMethod.POST})
    public Result<PageVo<OrderListDTO>> getToReceiveOrderList(@RequestBody PageBo pageBo){
        int studentId = StpUtil.getLoginIdAsInt();
        PageVo<OrderListDTO> toReceiveOrderList =  orderService.getStatusOrderList(pageBo, studentId, TaskConstant.ORDER_STATUS_TO_BE_RECEIVED);
        return Result.ok(toReceiveOrderList);
    }
    @ApiOperation("获取已完成订单列表")
    @RequestMapping(value = "getCompletedOrderList", method = {RequestMethod.POST})
    public Result<PageVo<OrderListDTO>> getCompletedOrderList(@RequestBody PageBo pageBo){
        int studentId = StpUtil.getLoginIdAsInt();
        PageVo<OrderListDTO> completedOrderList =  orderService.getStatusOrderList(pageBo, studentId, TaskConstant.ORDER_STATUS_COMPLETE);
        return Result.ok(completedOrderList);
    }

    @ApiOperation("获取已取消订单列表")
    @RequestMapping(value = "getCanceledOrderList", method = {RequestMethod.POST})
    public Result<PageVo<OrderListDTO>> getCanceledOrderList(@RequestBody PageBo pageBo){
        int studentId = StpUtil.getLoginIdAsInt();
        PageVo<OrderListDTO> canceledOrderList =  orderService.getCanceledOrderList(pageBo, studentId);
        return Result.ok(canceledOrderList);
    }

    @ApiOperation("获取待处理订单详情接口")
    @GetMapping(value = "/getPendingOrderDetail/{orderId}")
    public Result<OrderDetailPendingDTO> getPendingOrderDetail(@PathVariable int orderId){
        Order order = orderService.selectOrderById(orderId);
        Commodity commodity = studentPointsService.selectCommodityById(order.getCommodityId());
        String province = regionService.selectProvinceNameById(order.getProvinceId());
        String city = regionService.selectCityNameById(order.getCityId());
        String area = regionService.selectAreaNameById(order.getAreaId());
        OrderDetailPendingDTO dto = new OrderDetailPendingDTO();
        dto.setOrderId(order.getOrderId());
        dto.setOrderNumber(order.getOrderNumber());
        dto.setCommodityName(commodity.getCommodityName());
        dto.setImageUrl(SystemConstant.aliyunOSSUrlPrefix + commodity.getImageUrl());
        dto.setNotes(order.getNotes());
        dto.setPointsUsed(order.getPointsUsed());
        dto.setContactName(order.getContactName());
        dto.setContactPhone(order.getContactPhone());
        dto.setFullAddress(province + " " + city + " " + area + " " + order.getAddress());
        dto.setOrderData(order.getOrderDate());
        return Result.ok(dto);
    }

    @ApiOperation("获取待收货订单详情接口")
    @GetMapping(value = "/getToReceiveOrderDetail/{orderId}")
    public Result<OrderDetailToReceiveDTO> getToReceiveOrderDetail(@PathVariable int orderId){
        Order order = orderService.selectOrderById(orderId);
        Commodity commodity = studentPointsService.selectCommodityById(order.getCommodityId());
        String province = regionService.selectProvinceNameById(order.getProvinceId());
        String city = regionService.selectCityNameById(order.getCityId());
        String area = regionService.selectAreaNameById(order.getAreaId());
        OrderDetailToReceiveDTO dto = new OrderDetailToReceiveDTO();
        dto.setOrderId(order.getOrderId());
        dto.setOrderNumber(order.getOrderNumber());
        dto.setCommodityName(commodity.getCommodityName());
        dto.setImageUrl(SystemConstant.aliyunOSSUrlPrefix + commodity.getImageUrl());
        dto.setNotes(order.getNotes());
        dto.setPointsUsed(order.getPointsUsed());
        dto.setContactName(order.getContactName());
        dto.setContactPhone(order.getContactPhone());
        dto.setFullAddress(province + " " + city + " " + area + " " + order.getAddress());
        dto.setOrderData(order.getOrderDate());
        dto.setShipDate(order.getShipDate());
        dto.setTrackingNumber(order.getTrackingNumber());
        dto.setDistributeMethod(order.getDistributeMethod());
        return Result.ok(dto);
    }

    @ApiOperation("获取已完成订单详情接口")
    @GetMapping(value = "/getCompletedOrderDetail/{orderId}")
    public Result<OrderDetailCompletedDTO> getCompletedOrderDetail(@PathVariable int orderId){
        Order order = orderService.selectOrderById(orderId);
        Commodity commodity = studentPointsService.selectCommodityById(order.getCommodityId());
        String province = regionService.selectProvinceNameById(order.getProvinceId());
        String city = regionService.selectCityNameById(order.getCityId());
        String area = regionService.selectAreaNameById(order.getAreaId());
        OrderDetailCompletedDTO dto = new OrderDetailCompletedDTO();
        dto.setOrderId(order.getOrderId());
        dto.setOrderNumber(order.getOrderNumber());
        dto.setCommodityName(commodity.getCommodityName());
        dto.setImageUrl(SystemConstant.aliyunOSSUrlPrefix + commodity.getImageUrl());
        dto.setNotes(order.getNotes());
        dto.setPointsUsed(order.getPointsUsed());
        dto.setContactName(order.getContactName());
        dto.setContactPhone(order.getContactPhone());
        dto.setFullAddress(province + " " + city + " " + area + " " + order.getAddress());
        dto.setOrderData(order.getOrderDate());
        dto.setShipDate(order.getShipDate());
        dto.setTrackingNumber(order.getTrackingNumber());
        dto.setDistributeMethod(order.getDistributeMethod());
        dto.setCompleteDate(order.getCompleteDate());
        return Result.ok(dto);
    }

    @ApiOperation("获取已取消订单详情接口")
    @GetMapping(value = "/getCanceledOrderDetail/{orderId}")
    public Result<OrderDetailCanceledDTO> getCanceledOrderDetail(@PathVariable int orderId){
        Order order = orderService.selectOrderById(orderId);
        Commodity commodity = studentPointsService.selectCommodityById(order.getCommodityId());
        OrderDetailCanceledDTO dto = new OrderDetailCanceledDTO();
        dto.setOrderId(order.getOrderId());
        dto.setOrderNumber(order.getOrderNumber());
        dto.setCommodityName(commodity.getCommodityName());
        dto.setPointsUsed(order.getPointsUsed());
        dto.setImageUrl(SystemConstant.aliyunOSSUrlPrefix + commodity.getImageUrl());
        dto.setNotes(order.getNotes());
        dto.setOrderData(order.getOrderDate());
        dto.setCancelDate(order.getCancelDate());
        dto.setCancelReason(order.getCancelReason());
        return Result.ok(dto);
    }

    @ApiOperation("取消订单接口")
    @RequestMapping(value = "canceledOrder", method = {RequestMethod.POST})
    public Result<String> getCanceledOrderList(@RequestBody CancelOrderDTO dto){
        int studentId = StpUtil.getLoginIdAsInt();
        Order order = orderService.selectOrderById(dto.getOrderId());
        orderService.updateCancelInfo(dto.getOrderId(),dto.getCancelReason());
        studentPointsService.updateStudentPoints(studentId,order.getPointsUsed(),0);
        return Result.ok("取消订单成功");
    }

    @ApiOperation("确认收货接口")
    @GetMapping(value = "/confirmReception/{orderId}")
    public Result<String> confirmReception(@PathVariable int orderId){
        int studentId = StpUtil.getLoginIdAsInt();
        Order order = orderService.selectOrderById(orderId);
        orderService.updateCompleteInfo(order.getOrderId());
        ExchangeRecord exchangeRecord = new ExchangeRecord();
        exchangeRecord.setStudentId(studentId);
        exchangeRecord.setCommodityId(order.getCommodityId());
        exchangeRecord.setPointsUsed(order.getPointsUsed());
        studentPointsService.insertExchangeRecord(exchangeRecord);
        return Result.ok("确认收货成功");
    }


}
